package com.example.intermediate_control;

import androidx.appcompat.app.AppCompatActivity;

import android.app.TimePickerDialog;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.TimePicker;

import java.util.Calendar;


public class TimePickerActivity extends AppCompatActivity implements View.OnClickListener, TimePickerDialog.OnTimeSetListener {

    private TextView tvTime;
    private TimePicker tpTime;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_time_picker);
        tvTime = findViewById(R.id.tv_time);
        tpTime = findViewById(R.id.tp_time);
        tpTime.setIs24HourView(true);
        findViewById(R.id.btn_timeSelect).setOnClickListener(this);
        findViewById(R.id.btn_timeok).setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_timeSelect:
                //获取日历的一个实例,从中获取时分秒
                Calendar calendar = Calendar.getInstance();
                //切换为时钟的模式,构建一个时间对话框
                //如果不要时钟，还想要原来的那种显示方式,可以将第二个参数切换为主题Theme_Holo_Light_Dialog
                TimePickerDialog dialog = new TimePickerDialog(this,this,Calendar.HOUR_OF_DAY,Calendar.MINUTE,true);
                dialog.show();
                break;
            case R.id.btn_timeok:
                //直接获取时间,注入到文本中去显示
                String desc = String.format("您获取到的时间是%d时%d分", tpTime.getHour(),tpTime.getMinute());
                tvTime.setText(desc);

                break;
        }
    }

    @Override
    public void onTimeSet(TimePicker timePicker, int hour, int minute) {
        String desc = String.format("您获取到的时间是%d时%d分", hour,minute);
        tvTime.setText(desc);
    }
}